package cn.edu.yzu.scholarship.controller;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import net.sf.dynamicreports.jasper.builder.JasperReportBuilder;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

import cn.edu.yzu.scholarship.biz.ReportBiz;
import cn.edu.yzu.scholarship.constant.C;
import cn.edu.yzu.scholarship.kit.Sak;
import cn.edu.yzu.scholarship.model.Grant;
import cn.edu.yzu.scholarship.render.FileExtendRender;
import cn.edu.yzu.scholarship.render.JRImageRender;

import com.alibaba.fastjson.JSON;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Record;

public class ExportCtrler extends Controller {
	
	public static final String DEFAULT_JASPER_PRINT_LIST_SESSION_ATTRIBUTE = "net.sf.jasperreports.j2ee.jasper_print_list";
	public static final String DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE = "net.sf.jasperreports.j2ee.jasper_print";

	@SuppressWarnings("unchecked")
	public void index() throws Throwable {
		List<Map<String,Object>> data = (List<Map<String,Object>>)JSON.parseObject(getPara("data"),List.class);
		String[] columns = getPara("columns").split(",");
		System.out.println(columns.length);
		String title = getPara("title");
		String fileName = UUID.randomUUID() + ".xls";
		
		String absPath = C.GRANT_FOLDER + fileName;
		
		ReportBiz.exportExcel(absPath ,title, columns, data, buildFooter(data));
		
		setSessionAttr("grantFile", fileName);
		setSessionAttr("grantAbsPath", absPath);
		setSessionAttr("grantTitle", title);
		setSessionAttr("grantData", data);
		setSessionAttr("previewReport",ReportBiz.previewJasperReport(title, columns, data));
		
		//setSessionAttr("ouputReport",ReportBiz.exportExcel(title, columns, data));
		//setSessionAttr(DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, ReportBiz.fillJasperReport(title, columns, data).toJasperPrint());
        renderJson("message", "OK");
	}
	

	public void preview() {
		render(new JRImageRender((JasperReportBuilder) this.getSessionAttr("previewReport")));
		//render(new JRFlashRender());
	}
	
	@SuppressWarnings("unchecked")
	public void excel() {
		Grant.recordGrant((Record)getSessionAttr("user"), (String)getSessionAttr("grantTitle"), 
				(String)getSessionAttr("grantFile"), (List<Map<String,Object>>)getSessionAttr("grantData"));
		render(new FileExtendRender((String)getSessionAttr("grantTitle"), new File((String) this.getSessionAttr("grantAbsPath"))));
		removeSessionAttr("grantFile");
		removeSessionAttr("grantAbsPath");
		removeSessionAttr("grantTitle");
		removeSessionAttr("grantData");
		removeSessionAttr("previewReport");
		//render(new JRExcelRender((JasperReportBuilder) getSessionAttr("ouputReport")).fileName((String)getSessionAttr("grantTitle")));
	}
	

	private List<String> buildFooter(List<Map<String,Object>> data) {
		List<String> footers = Sak.newArrayList();
		double count = count(data,"scholarshipMoney");
		footers.add(" 合计：" + Sak.toRMB(count) + "(￥" + count + ")");
		footers.add(" 制表人：   " + StringUtils.repeat(" ", 16) + " 审批人： " + StringUtils.repeat(" ", 40) + new SimpleDateFormat("yyyy年 MM月 dd日").format(new Date()));
		return footers;
	}
	
	private double count(List<Map<String,Object>> data, String column) {
		double count = 0;
		for(Map<String,Object> map : data) {
			count += NumberUtils.toDouble(String.valueOf(map.get(column)), 0);
		}
		return count;
	}
	
}
